package com.xtq.springcloud.controller;

import com.xtq.springcloud.entity.Member;
import com.xtq.springcloud.entity.Result;
import com.xtq.springcloud.service.IMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: xietianqi
 * @create: 2024-01-22 23:01
 * @description: Controller
 **/

@Slf4j
@RestController // 有返回json的功能哈!
public class MemberController {
	@Resource
	private IMemberService memberService;
	
	/**
	 * 前端应该如何提交数据呢??
	 * <p>
	 * #1  如果前端是json格式的数据发送添加的信息,我们需要使用@RequestBody
	 * 才能将数据封装到相应的bean,同时报保证http请求头,content-type是对应
	 * <p>
	 * #2  如果前端以表单形式提交数据的话就不需要用RequestBody才能将数据封装到相应的bean,否则就封装不进去虽然不报错但数据库里全是null
	 * 同时报保证http请求头,content-type是对应
	 * @param member 提交的数据
	 * @return 返回结果给前端
	 */
	// @ResponseBody // 使其能返回json格式的数据
	@PostMapping("/member/save")
	public Result save(@RequestBody/*这个加上才可以正确接收json,表单和参数不适用!!!*/ Member member) {
		
		log.info("service-provider member={}",member);
		int save = memberService.save(member);
		if (save > 0) {
			return Result.success("添加会员成功", save);
		}
		return Result.error("401", "添加会员失败");
	}
	
	@GetMapping("/member/get/{id}")
	public Result getMemberById(@PathVariable ("id") Long id){
		Member member = memberService.querMemberById(id);
		if (member!=null){
			return Result.success("查询会员成功 服务MEMBER-SERVICE-PROVIDER-10000",member);
		}return Result.error("402","ID"+ id +"查无此人");
	}
	
}
